package com.ktgj.other.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.core.domain.R;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Customer;
import com.ktgj.other.domain.vo.EtSceneryCollectVo;
import com.ktgj.other.domain.vo.SceneryListVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.other.domain.EtCollect;
import com.ktgj.other.service.IEtCollectService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 会员收藏景点Controller
 * 
 * @author ruoyi
 * @date 2022-07-25
 */
@RestController
@RequestMapping("/collect")
public class EtCollectController extends BaseController
{
    @Autowired
    private IEtCollectService etCollectService;
    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询会员收藏景点列表
     */
    @PreAuthorize(hasPermi = "system:collect:list")
    @GetMapping("/list")
    public TableDataInfo list(EtCollect etCollect)
    {
        startPage();
        List<EtCollect> list = etCollectService.selectEtCollectList(etCollect);
        return getDataTable(list);
    }

    /**
     * 导出会员收藏景点列表
     */
    @PreAuthorize(hasPermi = "system:collect:export")
    @Log(title = "会员收藏景点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EtCollect etCollect) throws IOException
    {
        List<EtCollect> list = etCollectService.selectEtCollectList(etCollect);
        ExcelUtil<EtCollect> util = new ExcelUtil<EtCollect>(EtCollect.class);
        util.exportExcel(response, list, "会员收藏景点数据");
    }

    /**
     * 获取会员收藏景点详细信息
     */
    @PreAuthorize(hasPermi = "system:collect:query")
    @GetMapping(value = "/{etcollectid}")
    public AjaxResult getInfo(@PathVariable("etcollectid") Long etcollectid)
    {
        return AjaxResult.success(etCollectService.selectEtCollectById(etcollectid));
    }

    /**
     * 新增会员收藏景点
     */
    @PreAuthorize(hasPermi = "system:collect:add")
    @Log(title = "会员收藏景点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EtCollect etCollect)
    {
        return toAjax(etCollectService.insertEtCollect(etCollect));
    }

    /**
     * 修改会员收藏景点
     */
    @PreAuthorize(hasPermi = "system:collect:edit")
    @Log(title = "会员收藏景点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EtCollect etCollect)
    {
        return toAjax(etCollectService.updateEtCollect(etCollect));
    }

    /**
     * 删除会员收藏景点
     */
    @PreAuthorize(hasPermi = "system:collect:remove")
    @Log(title = "会员收藏景点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{etcollectids}")
    public AjaxResult remove(@PathVariable Long[] etcollectids)
    {
        return toAjax(etCollectService.deleteEtCollectByIds(etcollectids));
    }

    /**
     * 会员收藏景点列表
     * */
    @PostMapping("/collectList")
    public AjaxResult collectList(HttpServletRequest request, @RequestBody SceneryListVo sceneryListVo){
        String token = request.getHeader("token");
        Customer customer = remoteCustomerService.getLoginCustomerByToken(token).getData();
        if (customer != null){
            return AjaxResult.error("请重新登陆");
        }
        List<EtSceneryCollectVo> etSceneryCollectVo = etCollectService.collectList(customer.getCustomerId(), sceneryListVo);
        return AjaxResult.success(etSceneryCollectVo);
    }

    /**
     * 收藏景点
     * */
    @PostMapping("/collectAttractions")
    public AjaxResult collectAttractions(HttpServletRequest request, @RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        String token = request.getHeader("token");
        Customer customer = remoteCustomerService.getLoginCustomerByToken(token).getData();
        if (customer != null){
            return AjaxResult.error("请重新登陆");
        }
        int i = etCollectService.collectAttractions(customer.getCustomerId(), id);
        if (i > 0){
            return AjaxResult.success("添加收藏成功");
        }
        else {
            return AjaxResult.error("添加收藏失败");
        }
    }

    /**
     * 取消收藏
     * */
    @PostMapping("/cancel")
    public AjaxResult cancel(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        int i = etCollectService.deleteEtCollectById(id);
        if (i > 0){
            return AjaxResult.success("取消成功");
        }
        else {
            return AjaxResult.error("取消失败");
        }
    }
}
